
%%%%% PRELIMINARIES %%%%%
%%% This performs the main analysis as well as robustness checks that
%%% involve the "OUT" network definition.


%%% This uses the following sub-functions
% CreateMats_Mar2022.m
% Network_EM_Mar2022.m
% PE_EM_Mar2022.m
% Estimate_NoA_Mar2022.m
% Sim_Mar2022.m
% Run_Pref_Sims_Mar2022.m
% Variance_EM_Mar2022.m
% Tables_Mar2022.m
% GMM_Fn_Mar2022.m
% Fn_EstimateNetwork_Mar2022.m
% Fn_ImputeNetwork_Mar2022.m
% Fn_EstimatePE_Mar2022.m
% Fn_ImputsPE_Mar2022.m
% parsave_b.m
% parload_b.m
% matrix2latex.m

%%% and the following Datasets
% G_ij_T2C.csv
% G_ij_T1.csv
% Indiv_T2C.csv
% Indiv_T1.csv
% sch_size_T2C.csv
% sch_size_T1.csv



clear

%%% Settings
% Restart = 0 if running from 0, =1 if restarting part (2) below
Restart = 0
% Settings for Network EM algorithm (part (2) below)
chains = 2
sim_tol = 1e-2
reps_chain = 30

% Settings for Peer Effects EM algorithm (part (3) below)
PE_sim_reps = 20
burnin = 10
b_gap = 5

% Settings for simple model (no A's) (part (4) below)
chains_NoA = 10

% Settings for Simulations (parts (6) and (7) below
Sim_reps = 20


%%% Run these in order
% (1) Create variable datasets
if Restart==0
     T2C = 1
     CreateMats_Mar2022
   
     T2C = 0
     CreateMats_Mar2022
end


% (2) Estimate Network Formation Parameters
    % Note that this process may freeze / get timed out, since it takes a 
    % long time. set Restart = 1 to Restart the process.
    
    Network_EM_Mar2022


% (3) Peer Effects cond'l on Network Parameters   
    % Main (OR) network def
    Net_OUT = 0
    PE_EM_Mar2022
    
    % Alternative (OUT) network def   
    Net_OUT = 1
    PE_EM_Mar2022


% (4) Simple model (no A's)
    % OR Network
    Net_OUT = 0
    Model = 1
    Estimate_NoA_Mar2022
    
    Model = 2
    Estimate_NoA_Mar2022
    
    % OUT Network
    Net_OUT = 1
    Model = 1
    Estimate_NoA_Mar2022
    
    Model = 2
    Estimate_NoA_Mar2022        

% (5) Calculate Variance
    Net_OUT = 0
    Variance_EM_Mar2022

    Net_OUT = 1
    Variance_EM_Mar2022

% (6) Simulate to compare to T1
    T2C = 0
    P_random = 0
    
    Net_OUT = 0
    Sim_Mar2022
    save(['Sims_T1_',num2str(sim_tol),'_',num2str(Sim_reps),'_OR.mat'],'Sims_Out'); 

    Net_OUT = 1
    Sim_Mar2022
    save(['Sims_T1_',num2str(sim_tol),'_',num2str(Sim_reps),'_OUT.mat'],'Sims_Out'); 

% (7) Simulate preferred assignments
    T2C = 1
    P_random = 1
    
    Net_OUT = 0
    Run_Pref_Sims_Mar2022

    Net_OUT = 1
    Run_Pref_Sims_Mar2022



% (8) Create Tables
    Net_OUT = 0
    Tables_Mar2022

    Net_OUT = 1
    Tables_Mar2022


